<?php
include "header.php";
include '../date_utils.php';

if(!User::isAutentified()) { 
  header('Location:../index.php');
  exit();
} 


if(isset($_GET['place_id'])) {
  $place_id = htmlspecialchars($_GET['place_id']); 
} else if(isset($_POST['place_id'])) {
  $place_id = htmlspecialchars($_POST['place_id']);
} else {
  exit; 
}

// get place info
$place_query = mysql_query("SELECT * FROM places WHERE id='$place_id' LIMIT 1");
if(mysql_num_rows($place_query) != 1) { exit; }
$place = mysql_fetch_assoc($place_query);

if(!User::isAdmin() && ($place[id_user] != User::id())) {
	header('Location:./index.php?alert=Esta protesta no te pertenece');
	exit();	
}



// do place edit if requested
if($task == "doedit") {
			
 $title = parseInput($_POST['title']);
 $type = parseInput($_POST['type']);
 $address = parseInput($_POST['address']);
 $uri = parseInput($_POST['uri']);
 $description = parseInput($_POST['description'], true);
 $start_date = parseInput($_POST['start_date']);
 $end_date = parseInput($_POST['end_date']);
 $hashtag = parseInput($_POST['hashtag']);
 $lat = parseInput($_POST['lat']);
 $lng = parseInput($_POST['lng']);
	
	$today=date("Y-m-d");
	$separator = getSeparator($start_date);
	$mysql_start_date = convertDateToDbFormat( $start_date, $separator );
	$mysql_end_date = convertDateToDbFormat( $end_date, $separator );

	// validate fields
	$errors = "";
	if(empty($title) || empty($type) || empty($address) || empty($description) || empty($start_date) || empty($end_date)) {
	  $errors = "Se requieren al menos los campos obligatorios.";
	} elseif(datecheck($start_date, $separator)===false) {
	    $errors =  "La fecha de inicio no es correcta";
	} elseif(datecheck($end_date, $separator)===false) { 
	    $errors = "La fecha de fin no es correcta";
//	} elseif($today > $mysql_start_date) {
//	  	$errors = "La fecha de hoy es mayor a la del inicio de la protesta";
	} elseif($mysql_start_date > $mysql_end_date) {
			$errors = "Error, fecha de inicio mayor a la de fin.";
	} else {    

		
		  mysql_query("UPDATE places SET title='$title', type='$type', address='$address', uri='$uri', description='$description', start_date='$mysql_start_date', end_date='$mysql_end_date', hashtag='$hashtag', lng='$lng', lat='$lat' WHERE id='$place_id' LIMIT 1") or die(mysql_error());
 
		    
	    
	  // geocode
	  $hide_geocode_output = true;
	  include "../geocode.php";
	  
	  header("Location: index.php?view=$view&search=$search&p=$p");
	  exit;
	}
	
}

?>

<? echo $admin_head; ?>


<form id="admin" class="form-horizontal" action="edit.php" method="post">
  <h2>
    Editar Protesta
  </h2>
  <div id="result"></div>
  <fieldset>
  <?php if(User::isAdmin()) { ?>
    <div class="control-group">
      <label class="control-label" for="">Email</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="title" value="<?=$place[owner_email]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Latitud</label>
      <div class="controls">
        <input type="text" class="input input-medium" name="lat" value="0" id="">
        <label ><?=$place[lat]?></label>
      </div>
    </div>
        <div class="control-group">
      <label class="control-label" for="">Longitud</label>
      <div class="controls">
        <input type="text" class="input input-medium" name="lng" value="0" id="">
        <label ><?=$place[lng]?></label>
      </div>
    </div>
    <?php  } ?>
    <div class="control-group">
      <label class="control-label" for="">Nombre Protesta</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="title" value="<?=$place[title]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Tipo Protesta</label>
      <div class="controls">
        <select class="input input-xlarge" name="type">
		<?php foreach($places_types as $type){ ?>
    		<option<? if($place[type] == $type[0]) {?> selected="selected"<? } ?> value="<?=$type[0]?>"><?=$type[1]?></option>
		<?php } ?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Ubicación</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="address" value="<?=$place[address]?>" id="address">
        <button class="btn btn-info" id="search-address"><i class="icon-search"></i></button>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Descripción</label>
      <div class="controls">
        <textarea class="input input-xlarge" name="description" maxLength="<?php echo $place_desc_maxlength; ?>"><?=unparseInput($place[description])?></textarea>
         <p class="help-block" id="help">
             Max <?php echo $place_desc_maxlength; ?> caracteres.
         </p>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Fecha de Inicio</label>
      <div class="controls">
        <input type="text" class="input input-small" name="start_date" id="start_date" value="<?=convertDateToUserFormat($place[start_date],"-")?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Fecha de Fin</label>
      <div class="controls">
        <input type="text" class="input input-small" name="end_date" id="end_date" value="<?=convertDateToUserFormat($place[end_date],"-")?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">URL del sitio</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="uri" value="<?=$place[uri]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Hashtag</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="hashtag" value="<?=$place[hashtag]?>" id="">
      </div>
    </div>
    <div class="form-actions">
      <button type="submit" class="btn btn-primary">Guardar Cambios</button>
      <input type="hidden" name="task" value="doedit" />
      <input type="hidden" name="place_id" value="<?=$place[id]?>" />
      <input type="hidden" name="view" value="<?=$view?>" />
      <input type="hidden" name="search" value="<?=$search?>" />
      <input type="hidden" name="p" value="<?=$p?>" />
      <a href="index.php" class="btn" style="float: right;">Cancelar</a>
    </div>
  </fieldset>
</form>


		<link rel="stylesheet" href="../css/datepicker.css" type="text/css" />
    <script type="text/javascript" src="../scripts/bootstrap-datepicker.js"></script>
    <script type="text/javascript" src="../scripts/config-datepicker.js"></script>

    <script>
    	$("#search-address").click(function(e){
    		var address = $("#address").val();
    		e.preventDefault();
    		window.open("https://maps.google.com/maps?q="+address, "search-address");
    	});
    	
    	errors="<?php echo $errors; ?>";
			if(errors != ""){
					$("#admin #result").html(errors); 
					$("#admin #result").addClass("alert alert-danger");
			}

		</script>

<? echo $admin_foot; ?>
